using System;
using Data;
using Framework.GamePool;
using Framework.UI;
using IcecreamView;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;

namespace View.Component
{
    public class CubeItem : MonoBehaviour , IDragHandler , IPointerUpHandler , IPointerDownHandler
    {

        public TextMeshProUGUI textMeshProUgui;
        
        private int Count;
        
        public GameObjectType ObjectType;

        private GameLightCube _cube;

        private void OnEnable()
        {
            UIManager.Instance.EventManager.BindEvent(EventCode.RecoveryCube , this.OnRecovery);
        }

        private void OnDisable()
        {
            UIManager.Instance.EventManager.UnBindEvent(EventCode.RecoveryCube , this.OnRecovery);
        }

        private void OnRecovery(EventArg obj)
        {
            GameObjectType ty = obj.GetValue<GameObjectType>();
            if (ty == ObjectType)
            {
                this.Count++;
                this.textMeshProUgui.text = this.Count + "";
            }
        }

        public void UpdateItem()
        {
            this.Count = GameManager.Instance.GameRoom.GameRoomData.GetCubeCount(this.ObjectType);
            textMeshProUgui.text = this.Count + "";
            if (this.Count == 0)
            {
                gameObject.SetActive(false);
            }
            else
            {
                gameObject.SetActive(true);
            }

        }

        public void OnDrag(PointerEventData eventData)
        {
            if (!this._cube)
                return;
            Ray        ray = GameManager.Instance.MainCamera.ScreenPointToRay(eventData.position);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit , 100 , 1 << 8))
            {
                this._cube.transform.position = hit.point + Vector3.up * this._cube.hight;
            }
            
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            if (!this._cube)
                return;
            if (GameManager.Instance.GameRoom.AddCube(this._cube) == false)
            {
                this._cube.Pool_Disable();
                // Debug.Log("销毁对象： " + this.ObjectType);
            }
            else
            {
                this.Count--;
                this.textMeshProUgui.text = this.Count + "";
            }
            this._cube = null;
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            if (this.Count == 0)
            {
                return;
            }
            this._cube = GamePoolMgr.Instance.InstantiatePoolObject<GameLightCube>(this.ObjectType.ToString(), GameManager.Instance.World);
            Ray        ray = GameManager.Instance.MainCamera.ScreenPointToRay(eventData.position);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit , 100 , 1 << 8))
            {
                this._cube.transform.position = hit.point + Vector3.up * this._cube.hight;
            }
        }
    }
}